home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / ieee-utils / test.c < prev   
Encoding:
C/C++ Source or Header  |  2001-11-01  |  14.7 KB  |  465 lines

  1. /* ieee-utils/test.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <float.h>
  24. #include <string.h>
  25. #include <gsl/gsl_ieee_utils.h>
  26. #include <gsl/gsl_test.h>
  27.  
  28. #if defined(HAVE_IRIX_IEEE_INTERFACE)
  29. /* don't test denormals on IRIX */
  30. #else
  31. #define TEST_DENORMAL 1
  32. #endif
  33.  
  34. int
  35. main (void)
  36. {
  37.   float zerof = 0.0f, minus_onef = -1.0f ;
  38.   double zero = 0.0, minus_one = -1.0 ;
  39.  
  40.   /* Check for +ZERO (float) */
  41.  
  42.   {
  43.     float f = 0.0f;
  44.     const char mantissa[] = "00000000000000000000000";
  45.     gsl_ieee_float_rep r;
  46.     gsl_ieee_float_to_rep (&f, &r);
  47.  
  48.     gsl_test_int (r.sign, 0, "float x = 0, sign is +");
  49.     gsl_test_int (r.exponent, -127, "float x = 0, exponent is -127");
  50.     gsl_test_str (r.mantissa, mantissa, "float x = 0, mantissa");
  51.     gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = 0, type is ZERO");
  52.   }
  53.  
  54.   /* Check for -ZERO (float) */
  55.  
  56.   {
  57.     float f = minus_onef;
  58.     const char mantissa[] = "00000000000000000000000";
  59.     gsl_ieee_float_rep r;
  60.     
  61.     while (f < 0) {
  62.       f *= 0.1f;
  63.     }
  64.  
  65.     gsl_ieee_float_to_rep (&f, &r);
  66.  
  67.     gsl_test_int (r.sign, 1, "float x = -1*0, sign is -");
  68.     gsl_test_int (r.exponent, -127, "float x = -1*0, exponent is -127");
  69.     gsl_test_str (r.mantissa, mantissa, "float x = -1*0, mantissa");
  70.     gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = -1*0, type is ZERO");
  71.   }
  72.  
  73.   /* Check for a positive NORMAL number (e.g. 2.1) (float) */
  74.  
  75.   {
  76.     float f = 2.1f;
  77.     const char mantissa[] = "00001100110011001100110";
  78.  
  79.     gsl_ieee_float_rep r;
  80.     gsl_ieee_float_to_rep (&f, &r);
  81.  
  82.     gsl_test_int (r.sign, 0, "float x = 2.1, sign is +");
  83.     gsl_test_int (r.exponent, 1, "float x = 2.1, exponent is 1");
  84.     gsl_test_str (r.mantissa, mantissa, "float x = 2.1, mantissa");
  85.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 2.1, type is NORMAL");
  86.   }
  87.  
  88.  
  89.   /* Check for a negative NORMAL number (e.g. -1.3304...) (float) */
  90.  
  91.   {
  92.     float f = -1.3303577090924210f ;
  93.     const char mantissa[] = "01010100100100100101001";
  94.  
  95.     gsl_ieee_float_rep r;
  96.     gsl_ieee_float_to_rep (&f, &r);
  97.  
  98.     gsl_test_int (r.sign, 1, "float x = -1.3304..., sign is -");
  99.     gsl_test_int (r.exponent, 0, "float x = -1.3304..., exponent is 0");
  100.     gsl_test_str (r.mantissa, mantissa, "float x = -1.3304..., mantissa");
  101.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
  102.           "float x = -1.3304..., type is NORMAL");
  103.   }
  104.  
  105.   /* Check for a large positive NORMAL number (e.g. 3.37e31) (float) */
  106.  
  107.   {
  108.     float f = 3.37e31f;
  109.     const char mantissa[] = "10101001010110101001001";
  110.     gsl_ieee_float_rep r;
  111.     gsl_ieee_float_to_rep (&f, &r);
  112.  
  113.     gsl_test_int (r.sign, 0, "float x = 3.37e31, sign is +");
  114.     gsl_test_int (r.exponent, 104, "float x = 3.37e31, exponent is 104");
  115.     gsl_test_str (r.mantissa, mantissa, "float x = 3.37e31, mantissa");
  116.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 3.37e31, type is NORMAL");
  117.   }
  118.  
  119.   /* Check for a small positive NORMAL number (e.g. 3.37e-31) (float) */
  120.  
  121.   {
  122.     float f = 3.37e-31f;
  123.     const char mantissa[] = "10110101011100110111011";
  124.  
  125.     gsl_ieee_float_rep r;
  126.     gsl_ieee_float_to_rep (&f, &r);
  127.  
  128.     gsl_test_int (r.sign, 0, "float x = 3.37e-31, sign is +");
  129.     gsl_test_int (r.exponent, -102, "float x = 3.37e-31, exponent is -102");
  130.     gsl_test_str (r.mantissa, mantissa, "float x = 3.37e-31, mantissa");
  131.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
  132.           "float x = 3.37e-31, type is NORMAL");
  133.   }
  134.  
  135.   /* Check for FLT_MIN (smallest possible number that is not denormal) */
  136.  
  137.   {
  138.     float f = 1.17549435e-38f;    /* FLT_MIN (float) */
  139.     const char mantissa[] = "00000000000000000000000";
  140.     gsl_ieee_float_rep r;
  141.     gsl_ieee_float_to_rep (&f, &r);
  142.  
  143.     gsl_test_int (r.sign, 0, "float x = FLT_MIN, sign is +");
  144.     gsl_test_int (r.exponent, -126, "float x = FLT_MIN, exponent is -126");
  145.     gsl_test_str (r.mantissa, mantissa, "float x = FLT_MIN, mantissa");
  146.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MIN, type is NORMAL");
  147.   }
  148.  
  149.   /* Check for FLT_MAX (largest possible number that is not Inf) */
  150.  
  151.   {
  152.     float f = 3.40282347e+38f;    /* FLT_MAX */
  153.     const char mantissa[] = "11111111111111111111111";
  154.  
  155.     gsl_ieee_float_rep r;
  156.     gsl_ieee_float_to_rep (&f, &r);
  157.  
  158.     gsl_test_int (r.sign, 0, "float x = FLT_MAX, sign is +");
  159.     gsl_test_int (r.exponent, 127, "float x = FLT_MAX, exponent is 127");
  160.     gsl_test_str (r.mantissa, mantissa, "float x = FLT_MAX, mantissa");
  161.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MAX, type is NORMAL");
  162.   }
  163.  
  164.  
  165.   /* Check for DENORMAL numbers (e.g. FLT_MIN/2^n) */
  166.  
  167. #ifdef TEST_DENORMAL
  168.   {
  169.     float f = 1.17549435e-38f;    /* FLT_MIN */
  170.     char mantissa[] = "10000000000000000000000";
  171.  
  172.     int i;
  173.     gsl_ieee_float_rep r;
  174.  
  175.     for (i = 0; i < 23; i++)
  176.       {
  177.     float x = f / (float)pow (2.0, 1 + (float) i);
  178.     mantissa[i] = '1';
  179.     gsl_ieee_float_to_rep (&x, &r);
  180.  
  181.     gsl_test_int (r.sign, 0, "float x = FLT_MIN/2^%d, sign is +", i + 1);
  182.     gsl_test_int (r.exponent, -127,
  183.               "float x = FLT_MIN/2^%d, exponent is -127", i + 1);
  184.     gsl_test_str (r.mantissa, mantissa,
  185.               "float x = FLT_MIN/2^%d, mantissa", i + 1);
  186.     gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL,
  187.               "float x = FLT_MIN/2^%d, type is DENORMAL", i + 1);
  188.     mantissa[i] = '0';
  189.       }
  190.   }
  191. #endif
  192.  
  193.   /* Check for positive INFINITY (e.g. 2*FLT_MAX) */
  194.  
  195.   {
  196.     float f = 3.40282347e+38f;    /* FLT_MAX */
  197.     const char mantissa[] = "00000000000000000000000";
  198.  
  199.     gsl_ieee_float_rep r;
  200.  
  201.     float x;
  202.     x = 2 * f;
  203.     gsl_ieee_float_to_rep (&x, &r);
  204.  
  205.     gsl_test_int (r.sign, 0, "float x = 2*FLT_MAX, sign is +");
  206.     gsl_test_int (r.exponent, 128, "float x = 2*FLT_MAX, exponent is 128");
  207.     gsl_test_str (r.mantissa, mantissa, "float x = 2*FLT_MAX, mantissa");
  208.     gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF");
  209.   }
  210.  
  211.   /* Check for negative INFINITY (e.g. -2*FLT_MAX) */
  212.  
  213.   {
  214.     float f = 3.40282347e+38f;    /* FLT_MAX */
  215.     const char mantissa[] = "00000000000000000000000";
  216.  
  217.     gsl_ieee_float_rep r;
  218.  
  219.     float x;
  220.     x = -2 * f;
  221.     gsl_ieee_float_to_rep (&x, &r);
  222.  
  223.     gsl_test_int (r.sign, 1, "float x = -2*FLT_MAX, sign is -");
  224.     gsl_test_int (r.exponent, 128, "float x = -2*FLT_MAX, exponent is 128");
  225.     gsl_test_str (r.mantissa, mantissa, "float x = -2*FLT_MAX, mantissa");
  226.     gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF");
  227.   }
  228.  
  229.   /* Check for NAN (e.g. Inf - Inf) (float) */
  230.  
  231.   {
  232.     gsl_ieee_float_rep r;
  233.     float x = 1.0f, y = 2.0f, z = zerof;
  234.  
  235.     x = x / z;
  236.     y = y / z;
  237.     z = y - x;
  238.  
  239.     gsl_ieee_float_to_rep (&z, &r);
  240.  
  241.     /* We don't check the sign and we don't check the mantissa because
  242.        they could be anything for a NaN */
  243.  
  244.     gsl_test_int (r.exponent, 128, "float x = NaN, exponent is 128");
  245.     gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "float x = NaN, type is NAN");
  246.   }
  247.  
  248.  
  249.   /* Check for +ZERO */
  250.  
  251.   {
  252.     double d = 0.0;
  253.     const char mantissa[]
  254.       = "0000000000000000000000000000000000000000000000000000";
  255.     gsl_ieee_double_rep r;
  256.     gsl_ieee_double_to_rep (&d, &r);
  257.  
  258.     gsl_test_int (r.sign, 0, "double x = 0, sign is +");
  259.     gsl_test_int (r.exponent, -1023, "double x = 0, exponent is -1023");
  260.     gsl_test_str (r.mantissa, mantissa, "double x = 0, mantissa");
  261.     gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = 0, type is ZERO");
  262.   }
  263.  
  264.   /* Check for -ZERO */
  265.  
  266.   {
  267.     double d =  minus_one;
  268.     const char mantissa[]
  269.       = "0000000000000000000000000000000000000000000000000000";
  270.     gsl_ieee_double_rep r;
  271.  
  272.     while (d < 0) {
  273.       d *= 0.1;
  274.     }
  275.  
  276.     gsl_ieee_double_to_rep (&d, &r);
  277.  
  278.     gsl_test_int (r.sign, 1, "double x = -1*0, sign is -");
  279.     gsl_test_int (r.exponent, -1023, "double x = -1*0, exponent is -1023");
  280.     gsl_test_str (r.mantissa, mantissa, "double x = -1*0, mantissa");
  281.     gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = -1*0, type is ZERO");
  282.   }
  283.  
  284.   /* Check for a positive NORMAL number (e.g. 2.1) */
  285.  
  286.   {
  287.     double d = 2.1;
  288.     const char mantissa[]
  289.       = "0000110011001100110011001100110011001100110011001101";
  290.     gsl_ieee_double_rep r;
  291.     gsl_ieee_double_to_rep (&d, &r);
  292.  
  293.     gsl_test_int (r.sign, 0, "double x = 2.1, sign is +");
  294.     gsl_test_int (r.exponent, 1, "double x = 2.1, exponent is 1");
  295.     gsl_test_str (r.mantissa, mantissa, "double x = 2.1, mantissa");
  296.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = 2.1, type is NORMAL");
  297.   }
  298.  
  299.  
  300.   /* Check for a negative NORMAL number (e.g. -1.3304...) */
  301.  
  302.   {
  303.     double d = -1.3303577090924210146738460025517269968986511230468750;
  304.     const char mantissa[]
  305.       = "0101010010010010010100101010010010001000100011101110";
  306.     gsl_ieee_double_rep r;
  307.     gsl_ieee_double_to_rep (&d, &r);
  308.  
  309.     gsl_test_int (r.sign, 1, "double x = -1.3304..., sign is -");
  310.     gsl_test_int (r.exponent, 0, "double x = -1.3304..., exponent is 0");
  311.     gsl_test_str (r.mantissa, mantissa, "double x = -1.3304..., mantissa");
  312.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
  313.           "double x = -1.3304..., type is NORMAL");
  314.   }
  315.  
  316.   /* Check for a large positive NORMAL number (e.g. 3.37e297) */
  317.  
  318.   {
  319.     double d = 3.37e297;
  320.     const char mantissa[]
  321.       = "0100100111001001100101111001100000100110011101000100";
  322.     gsl_ieee_double_rep r;
  323.     gsl_ieee_double_to_rep (&d, &r);
  324.  
  325.     gsl_test_int (r.sign, 0, "double x = 3.37e297, sign is +");
  326.     gsl_test_int (r.exponent, 988, "double x = 3.37e297, exponent is 998");
  327.     gsl_test_str (r.mantissa, mantissa, "double x = 3.37e297, mantissa");
  328.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
  329.           "double x = 3.37e297, type is NORMAL");
  330.   }
  331.  
  332.   /* Check for a small positive NORMAL number (e.g. 3.37e-297) */
  333.  
  334.   {
  335.     double d = 3.37e-297;
  336.     const char mantissa[]
  337.     = "0001101000011011101011100001110010100001001100110111";
  338.     gsl_ieee_double_rep r;
  339.     gsl_ieee_double_to_rep (&d, &r);
  340.  
  341.     gsl_test_int (r.sign, 0, "double x = 3.37e-297, sign is +");
  342.     gsl_test_int (r.exponent, -985, "double x = 3.37e-297, exponent is -985");
  343.     gsl_test_str (r.mantissa, mantissa, "double x = 3.37e-297, mantissa");
  344.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
  345.           "double x = 3.37e-297, type is NORMAL");
  346.   }
  347.  
  348.   /* Check for DBL_MIN (smallest possible number that is not denormal) */
  349.  
  350.   {
  351.     double d = 2.2250738585072014e-308;        /* DBL_MIN */
  352.     const char mantissa[]
  353.       = "0000000000000000000000000000000000000000000000000000";
  354.     gsl_ieee_double_rep r;
  355.     gsl_ieee_double_to_rep (&d, &r);
  356.  
  357.     gsl_test_int (r.sign, 0, "double x = DBL_MIN, sign is +");
  358.     gsl_test_int (r.exponent, -1022, "double x = DBL_MIN, exponent is -1022");
  359.     gsl_test_str (r.mantissa, mantissa, "double x = DBL_MIN, mantissa");
  360.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
  361.           "double x = DBL_MIN, type is NORMAL");
  362.   }
  363.  
  364.   /* Check for DBL_MAX (largest possible number that is not Inf) */
  365.  
  366.   {
  367.     double d = 1.7976931348623157e+308;        /* DBL_MAX */
  368.     const char mantissa[]
  369.     = "1111111111111111111111111111111111111111111111111111";
  370.     gsl_ieee_double_rep r;
  371.     gsl_ieee_double_to_rep (&d, &r);
  372.  
  373.     gsl_test_int (r.sign, 0, "double x = DBL_MAX, sign is +");
  374.     gsl_test_int (r.exponent, 1023, "double x = DBL_MAX, exponent is 1023");
  375.     gsl_test_str (r.mantissa, mantissa, "double x = DBL_MAX, mantissa");
  376.     gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
  377.           "double x = DBL_MAX, type is NORMAL");
  378.   }
  379.  
  380.   /* Check for DENORMAL numbers (e.g. DBL_MIN/2^n) */
  381.  
  382. #ifdef TEST_DENORMAL
  383.   {
  384.     double d = 2.2250738585072014e-308;        /* DBL_MIN */
  385.     char mantissa[]
  386.       = "1000000000000000000000000000000000000000000000000000";
  387.     int i;
  388.     gsl_ieee_double_rep r;
  389.  
  390.     for (i = 0; i < 52; i++)
  391.       {
  392.     double x = d / pow (2.0, 1 + (double) i);
  393.     mantissa[i] = '1';
  394.     gsl_ieee_double_to_rep (&x, &r);
  395.  
  396.     gsl_test_int (r.sign, 0, "double x = DBL_MIN/2^%d, sign is +", i + 1);
  397.     gsl_test_int (r.exponent, -1023,
  398.               "double x = DBL_MIN/2^%d, exponent", i + 1);
  399.     gsl_test_str (r.mantissa, mantissa,
  400.               "double x = DBL_MIN/2^%d, mantissa", i + 1);
  401.     gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL,
  402.               "double x = DBL_MIN/2^%d, type is DENORMAL", i + 1);
  403.     mantissa[i] = '0';
  404.       }
  405.   }
  406. #endif
  407.  
  408.   /* Check for positive INFINITY (e.g. 2*DBL_MAX) */
  409.  
  410.   {
  411.     double d = 1.7976931348623157e+308;        /* DBL_MAX */
  412.     const char mantissa[]
  413.       = "0000000000000000000000000000000000000000000000000000";
  414.     gsl_ieee_double_rep r;
  415.  
  416.     double x;
  417.     x = 2.0 * d;
  418.     gsl_ieee_double_to_rep (&x, &r);
  419.  
  420.     gsl_test_int (r.sign, 0, "double x = 2*DBL_MAX, sign is +");
  421.     gsl_test_int (r.exponent, 1024, "double x = 2*DBL_MAX, exponent is 1024");
  422.     gsl_test_str (r.mantissa, mantissa, "double x = 2*DBL_MAX, mantissa");
  423.     gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "double x = 2*DBL_MAX, type is INF");
  424.   }
  425.  
  426.   /* Check for negative INFINITY (e.g. -2*DBL_MAX) */
  427.  
  428.   {
  429.     double d = 1.7976931348623157e+308;        /* DBL_MAX */
  430.     const char mantissa[]
  431.       = "0000000000000000000000000000000000000000000000000000";
  432.     gsl_ieee_double_rep r;
  433.  
  434.     double x;
  435.     x = -2.0 * d;
  436.     gsl_ieee_double_to_rep (&x, &r);
  437.  
  438.     gsl_test_int (r.sign, 1, "double x = -2*DBL_MAX, sign is -");
  439.     gsl_test_int (r.exponent, 1024, "double x = -2*DBL_MAX, exponent is 1024");
  440.     gsl_test_str (r.mantissa, mantissa, "double x = -2*DBL_MAX, mantissa");
  441.     gsl_test_int (r.type, GSL_IEEE_TYPE_INF,"double x = -2*DBL_MAX, type is INF");
  442.   }
  443.  
  444.   /* Check for NAN (e.g. Inf - Inf) */
  445.  
  446.   {
  447.     gsl_ieee_double_rep r;
  448.     double x = 1.0, y = 2.0, z = zero;
  449.  
  450.     x = x / z;
  451.     y = y / z;
  452.     z = y - x;
  453.  
  454.     gsl_ieee_double_to_rep (&z, &r);
  455.  
  456.     /* We don't check the sign and we don't check the mantissa because
  457.        they could be anything for a NaN */
  458.  
  459.     gsl_test_int (r.exponent, 1024, "double x = NaN, exponent is 1024");
  460.     gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "double x = NaN, type is NAN");
  461.   }
  462.  
  463.   exit (gsl_test_summary ());
  464. }
  465.